
#include <string>

#include "medialin.hpp"
//MediaLin stuff
using namespace boost::filesystem;
MediaLin::MediaLin(){
    errInt=0;
    count=0;
    errTxt="";
    getwd(curDir);
    doUpdate();


}

void MediaLin::chDir(char pathname[]){
    curDir[MAXPATHLEN]=pathname[MAXPATHLEN];
}
int file_select(const direct *entry){return !0;};
void MediaLin::doUpdate(){
    struct direct **files;
    puts("Reading directory.");
    if(false){
        puts("Clearing curDirItems");
        curDirItems.clear();
        puts("Cleared curDirItems");
    }
    
    if(is_directory(curDir)){
    count=scandir(curDir,&files,file_select,alphasort);
        for(int i=0;i<count;i++){
            //Images of bmp only right now

            if((strcmp(files[i]->d_name,".")!=0) &&
                (strcmp(files[i]->d_name,"..")!=0)){
                char* type="FIL";
                if(is_directory(files[i]->d_name)){
                    type="DIR";
                }
                std::string filename=files[i]->d_name;
                


                
                printf("%s\t%s\n",type,files[i]->d_name);
                ML_Item thisItem(filename);



            }

        }
    }
}

SDL_Surface * MediaLin::drawScreen(SDL_Surface * curScreen){
    SDL_Surface * newScreen = SDL_CreateRGBSurfaceFrom(curScreen->pixels,curScreen->w,curScreen->h,curScreen->offset,(int)curScreen->pitch,
            curScreen->format->Rmask,curScreen->format->Gmask,curScreen->format->Bmask,curScreen->format->Amask);
    

    SDLPango_Context * context = SDLPango_CreateContext();
    SDLPango_SetDefaultColor(context, MATRIX_TRANSPARENT_BACK_WHITE_LETTER);
    SDLPango_SetMinimumSize(context, 640, 0);
    SDLPango_SetMarkup(context, "This is <i>markup</i> text.", -1);
    SDLPango_Draw(context, newScreen, 10, 10);
    SDL_BlitSurface(newScreen,NULL,curScreen,NULL);
    SDL_FreeSurface(curScreen);
    SDLPango_FreeContext(context);
    return newScreen;
}

void  MediaLin::setup_opengl(int width, int height){
    //float ratio=(float) width/ (float) height;
    glShadeModel(GL_SMOOTH);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glEnable(GL_CULL_FACE);
    glClearColor(0,0,0,0);
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
}

ML_Provider::ML_Provider(){
    
}

ML_Provider::ML_Provider(string fileName){
    
}




// ML_Item stuff.

ML_Item::ML_Item(string filename){
    magic_t magicCookie = magic_open(MAGIC_SYMLINK|MAGIC_MIME|MAGIC_CONTINUE);
    magic_load(magicCookie,NULL);
    if(!is_directory(filename)){
        string fileTypeFound= magic_file(magicCookie,filename.c_str());


        if(strncmp(fileTypeFound.c_str(),"appli",5)==0){
            cout << "Is of video type."<<endl;
            provider = ML_Provider_Video(filename);
            type=ML_Item_VideoType;
        } else if(strncmp(fileTypeFound.c_str(),"audio",5)==0){
            cout << "Is of audio type."<<endl;
            type=ML_Item_AudioType;
        } else if(strncmp(fileTypeFound.c_str(),"image",5)==0){
            cout << "Is of image type."<<endl;
            type=ML_Item_ImageType;
        } else if(strncmp(fileTypeFound.c_str(),"text",4)==0){
            cout << "Is of text type."<<endl;
        } else {
            cout << "Is of unknown type "<<fileTypeFound<<"."<<endl;
        }
    } else {
        type=ML_Item_DirectoryType;
        cout << "Is of directory type."<<endl;
    }
    magic_close(magicCookie);
}


ML_Item::ML_Item(const ML_Item &copyin){
    type = copyin.type;
    fileLocation=copyin.fileLocation;
    size=copyin.size;

}

ML_Item & ML_Item::operator=(const ML_Item &rhs){
    this->size=rhs.size;
    this->fileLocation=rhs.fileLocation;
    this->type=rhs.type;
    return *this;
}

int ML_Item::operator==(const ML_Item& rhs) const{
    if(this->size.height!=rhs.size.height) return 0;
    if(this->size.width != rhs.size.width) return 0;
    if(this->fileLocation != rhs.fileLocation) return 0;
    if(this->type != rhs.type) return 0;
    return 1;
}

int ML_Item::operator <(const ML_Item& rhs) const{
    if(this->type < rhs.type){
        return 1;
    } else if((this->size.height*this->size.width)<(rhs.size.height*rhs.size.width)){
        return 1;
    } else {
        return 0;

    }

}